<template>
    <div class="login_container">
        <div class="login_body">
            <div class="avatar_body">
                <img src="../assets/img/login_icon.jpg" alt=" ">
            </div>
            <el-form  ref="formInline" :rules="formInlineRules" :inline="true" :model="formInline" class="login_form">
                <el-form-item prop="username">
                    <el-input style="width: 250px" v-model="formInline.username"  placeholder="&nbsp;请输入用户名"></el-input>
                </el-form-item>
                <br>
                <el-form-item prop="pwd">
                    <el-input style="width: 250px" v-model="formInline.pwd" type="password" placeholder="&nbsp;请输入密码"></el-input>
                </el-form-item>
                <br>

                <el-form-item class="buttons">
                    <el-button type="primary" @click="submitForm()">登&nbsp;录</el-button>&nbsp;&nbsp;&nbsp;&nbsp;
                    <el-button type="info" @click="resetForm()">重&nbsp;置</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button class="foot" type="text" @click="verificationVisible=true">手机验证码登录</el-button>
                    &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
                    <el-button class="foot" type="text" @click="registerVisible=true">新用户注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
<!--    手机验证码登录-->
    <el-dialog v-model="verificationVisible" title="手机验证码登录" width="40%" >
        <el-form :model="formVerificationInline" :rules="formInlineRules"  label-width="150px">
            <el-form-item prop="phone">
                <el-input style="width: 250px" v-model="formInline.phone"  placeholder="&nbsp;请输入手机号"></el-input>
            </el-form-item>
            <el-form-item prop="verificationCode">
                <el-input style="width: 145px"
                          type="password"
                          v-model="formInline.verificationCode"
                          placeholder="&nbsp;请输入手机验证码"></el-input>
                &nbsp;<el-button type="primary" @click="getVerificationCode" :disabled = "disabled">{{codeMsg}}</el-button>
            </el-form-item>
        </el-form>

        <template #footer>
      <span class="dialog-footer">
          <el-button type="primary" @click="submitForm()">登&nbsp;录</el-button>&nbsp;&nbsp;
          <el-button @click="verificationVisible = false">取&nbsp;消</el-button>
      </span>
        </template>
    </el-dialog>

<!--    注册-->
    <el-dialog v-model="registerVisible" title="新用户注册" width="40%" >
        <el-form :model="registerForms" :rules="formInlineRules" label-width="150px">
            <el-form-item prop="username">
                <el-input style="width: 250px" v-model="registerForms.name"  placeholder="&nbsp;请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="pwd">
                <el-input style="width: 250px" v-model="registerForms.pwd"  placeholder="&nbsp;请输入密码"></el-input>
            </el-form-item>
            <el-form-item prop="phone">
                <el-input style="width: 250px" v-model="registerForms.phone"  placeholder="&nbsp;请输入手机号"></el-input>
            </el-form-item>
            <el-form-item prop="verificationCode">
                <el-input style="width: 145px"
                          type="password"
                          v-model="registerForms.verificationCode"
                          placeholder="&nbsp;请输入手机验证码"></el-input>
                &nbsp;<el-button type="primary" @click="getVerificationCode" :disabled = "disabled">{{codeMsg}}</el-button>
            </el-form-item>
        </el-form>

        <template #footer>
      <span class="dialog-footer">
          <el-button type="primary" @click="submitForm()">注&nbsp;册</el-button>&nbsp;&nbsp;
          <el-button @click="registerVisible = false">取&nbsp;消</el-button>
      </span>
        </template>
    </el-dialog>

</template>

<script>
import API from '../network/login'
export default {
    data() {
        return {
            formVerificationInline: {        //验证码登录表单
                phone: '',
                verificationCode: '',
            },
            registerForms:{
                name:'',
                pwd:'',
                phone:'',
                verificationCode:''
            },
            formInline:{                 //密码登录表单
                username:'',
                pwd:''
            },
            disabled: false,
            registerVisible:false,
            verificationVisible: false,
            codeNum: 30,
            codeMsg: "获取验证码",
            formInlineRules: {
                username: [
                    {required: true, message: '用户名不能为空', trigger: 'blur'},
                ],
                pwd:[
                    {required: true, message: '密码不能为空', trigger: 'blur'},
                ],
                phone: [
                    {required: true, message: '手机号不能为空', trigger: 'blur'},
                    {min: 11, max: 11, message: '请输入正确的手机号', trigger: 'blur'}
                ],
                verificationCode: [
                    {required: true, message: '验证码不能为空', trigger: 'blur'},
                    {min: 6, max: 6, message: '请输入正确的验证码', trigger: 'blur'}
                ],
            }
        }
    },
    methods:{
         getVerificationCode() {
            // API.Login().then(res>{
            //
            // })

            API.SendMessage({phone:this.formInline.phone}).then(res=>{
                console.log(res)
            })
            this.disabled = true
            let timer = setInterval(()=>{
                --this.codeNum;
                this.codeMsg = `重新发送${this.codeNum}`
            },1000)
            setTimeout(()=>{
                clearInterval(timer)
                this.codeNum=30
                this.disabled = false
                this.codeMsg = "获取验证码"
            },30000)


        },
        submitForm() {
            // this.$refs.formInline.validate(async valid => {
            //     if (!valid) return;
            //     const {data: res} = await this.$http.post('login', this.formInline)
            //     if(res.meta.status !== 200) return this.$message.error("登录失败")
            //     this.$message.success("登录成功")
            //     window.sessionStorage.setItem("token",res.data.token);
            //     await this.$router.push("home")
            // })
            this.$refs.formInline.validate(valid => {
                if(valid) {
                    this.$message.success("登录成功")
                    setTimeout(()=>{
                        this.$router.push("home")
                    },2000)
                }
            })
        },
        resetForm() {
            this.$refs.formInline.resetFields();
        }
    }
}
</script>

<style scoped>
.login_container{
    width: 100%;
    height: 100%;
    background: url("../assets/img/bk.webp") no-repeat;
    background-size: 100% 100%;
}
.login_body{
    width: 450px;
    height: 300px;
    position: absolute;/*绝对定位*/
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);/*实现水平垂直居中*/
    opacity: 0.99;
}
.avatar_body{
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px ;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #eeeeee;

}
.avatar_body img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
}

.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 130px;
    /*box-sizing: border-box;*/
}

.buttons{
    display: flex;
    justify-content: flex-end;
}

.foot{
    font-size: 7px;
    font-weight: lighter;
    color: #333333;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>
